package com.xjzd.std.network.zerocopy;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;

public class FileChannelDemo {

    public static void main(String[] args) throws IOException {
        try (SocketChannel socketChannel = SocketChannel.open()) {
            socketChannel.configureBlocking(true);
            socketChannel.connect(new InetSocketAddress("127.0.0.1", 9999));

            long start = System.currentTimeMillis();
            long sum = 0, counts;
            try (FileInputStream fi = new FileInputStream("E:\\金职位\\21-项目React实现新闻网站核心功能.mp4")) {
                FileChannel fc = fi.getChannel();
                long total = fc.size();
                System.out.println(total);
                do {
                    counts = fc.transferTo(sum, total, socketChannel);
                    sum += counts;
                } while (sum < total);
            }
            System.out.println("copy " + sum + " bytes, cost " + (System.currentTimeMillis() - start) + "ms");
        }
    }
}
